{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Overview\n",
    "\n",
    "This example shows the different ways of spawning vehicles and objects through the python interface.\n",
    "\n",
    "Although spawning has been touched in other examples, this one will show you how to use ScenarioObject to spawn any object class and how to spawn vehicles AFTER scenario creation. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup and Scenario Creation\n",
    "\n",
    "This quick example uses the Smallgrid level to show the spawning mechanics of the library.\n",
    "It creates a roadblock pre-grame using the ScenarioObject class. \n",
    "Available assets can be found in the 'vehicle' directory in the BeamNG game root directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from beamngpy import BeamNGpy, Vehicle, Scenario, ScenarioObject\n",
    "\n",
    "beamng = BeamNGpy('localhost', 64256)\n",
    "beamng.open()\n",
    "\n",
    "scenario = Scenario('smallgrid', 'spawn_objects_example')\n",
    "\n",
    "vehicle = Vehicle('ego_vehicle', model='pickup', licence='PYTHON')\n",
    "scenario.add_vehicle(vehicle, pos=(0,0,0), rot=None, rot_quat=(0,0,0,1))\n",
    "\n",
    "scenario_obj = ScenarioObject(oid='roadblock', \n",
    "                              name='sawhorse',\n",
    "                              otype='BeamNGVehicle',\n",
    "                              pos=(0,-10,0),\n",
    "                              rot=None,\n",
    "                              rot_quat=(0,0,0,1),\n",
    "                              scale=(1, 1, 1),\n",
    "                              JBeam = 'sawhorse',\n",
    "                              datablock=\"default_vehicle\"\n",
    "                              )\n",
    "scenario.add_object(scenario_obj)\n",
    "\n",
    "scenario.make(beamng)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Starting the game"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "beamng.load_scenario(scenario)\n",
    "beamng.start_scenario()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## In-game spawning\n",
    "\n",
    "Spawning objects in-game is simple, though this feature is restricted to the vehicle class. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "vehicle = Vehicle('ego_vehicle3', model='etk800', color='White')\n",
    "beamng.spawn_vehicle(vehicle, pos=(0,-20, 0), rot=None, rot_quat=(0, 1, 0, 0))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.5 64-bit ('bng': venv)",
   "name": "python38564bitbngvenvd3ae9d403de3426ab664250421042199"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
